package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysGameRecord;
import com.ruoyi.system.domain.addCom;
import com.ruoyi.system.domain.member_info;
import com.ruoyi.system.service.ISysGameRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kaka.huang
 * @for study
 */
@RestController
@RequestMapping("/system/competition")
public class SysCompetitionController {
    @Autowired
    ISysGameRecordService iSysGameRecordService;
    /*
    * 获取竞赛列表信息*/
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list")
    public Map<String, Object> getOutstandingRecords(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                                     @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
                                                     @RequestParam(value = "postCode", required = false) String postCode,
                                                     @RequestParam(value = "postName", required = false) String postName,
                                                     @RequestParam(value = "status", required = false) String status) {
        HashMap<String, Object> ResData = new HashMap<>();
        try {
            Map<String, Object> result = iSysGameRecordService.selectCompetition(pageNum, pageSize ,postCode ,postName ,status);
            ResData.put("message","Success");
            ResData.put("code",200);
            ResData.put("data",result);
            return ResData;
        }catch (Exception e) {
            ResData.put("message","Error");
            ResData.put("code",400);
            return ResData;
        }
    }
//    查询组员接口
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping("/searchUser/{searchName}")
    public Map<String , Object> searchUser(@PathVariable String searchName){
        HashMap<String, Object> map = new HashMap<>();

        try {
            List<member_info> memberInfos = iSysGameRecordService.selectMemberInfo(searchName);
            map.put("data",memberInfos);
            map.put("code",200);
            return map;
        }catch (Exception e){
            map.put("message","Error");
            map.put("code",400);
            return map;
        }

    }
//新增方法
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @PostMapping("/add")
    public Map<String , Object> searchUser(@RequestBody addCom addCom){
        HashMap<String, Object> map = new HashMap<>();
        try {
            String game_id = iSysGameRecordService.addCompetition(addCom.getGame_name(), addCom.getTeam(), addCom.getGame_time(), addCom.getGame_result(), addCom.getTop());

            map.put("code",200);
            map.put("game_id",game_id);
            return map;
        }catch (Exception e){
            map.put("message","Error");
            map.put("code",400);
            return map;
        }

    }

    //提交证书
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @PostMapping("/submitPic")
    public Map<String , Object> submitPic(@RequestParam("game_id") String game_id,
                            @RequestPart("file") MultipartFile image , @RequestParam("certificate") String certificate) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            iSysGameRecordService.submitPic(game_id,image,certificate);
            map.put("code",200);
            return map;
        }catch (Exception e){
            map.put("message","Error");
            map.put("code",400);
            return map;
        }
    }

//    修改数据
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @PutMapping("/list")
    public Map<String , Object> UpdateCom(@RequestBody addCom addCom){
        HashMap<String, Object> map = new HashMap<>();
        try {
            iSysGameRecordService.UpdateCom(addCom.getGame_id(), addCom.getGame_name(), addCom.getTeam(), addCom.getGame_time(), addCom.getGame_result() , addCom.getTop());
            map.put("code",200);
            return map;
        }catch (Exception e){
            map.put("message","error");
            map.put("code",400);
            return map;
        }
    }

//    删除数据
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @DeleteMapping("/delete/{game_id}")
    public Map<String , Object> remove(@PathVariable String game_id)
    {
        HashMap<String, Object> map = new HashMap<>();
        try {
            iSysGameRecordService.remove(game_id);
            map.put("code",200);
            return map;
        }catch (Exception e){
            map.put("message","Error");
            map.put("code",400);
            return map;
        }
    }

}
